package mylog

import (
	"context"
	"go.uber.org/zap"
	"wx-uniapp-go/third_party/cdslog"
)

const (
	TaskID        = "task_id"
	Type          = "type"
	LogTraceIdKey = cdslog.LOGTRACEIDKEY
)

func W(ctx context.Context) *cdslog.CdsLogger {
	return WithCtx(ctx)
}

func WithCtx(ctx context.Context) *cdslog.CdsLogger {
	if ctx == nil {
		return cdslog.GetLogger()
	}
	nl := LogExt(cdslog.GetLogger(), ctx)
	return nl
}

func LogExt(ol *cdslog.CdsLogger, ctx context.Context) (nl *cdslog.CdsLogger) {
	nl = ol
	if ctx == nil {
		return
	}

	if v1, ok1 := ctx.Value(LogTraceIdKey).(string); ok1 {
		nl = nl.With(zap.String(LogTraceIdKey, v1))
	}
	if v1, ok1 := ctx.Value(LogTraceIdKey).(string); ok1 {
		nl = nl.With(zap.String(LogTraceIdKey, v1))
	}
	if v1, ok1 := ctx.Value(TaskID).(string); ok1 {
		nl = nl.With(zap.String(TaskID, v1))
	}
	if v1, ok1 := ctx.Value(Type).(string); ok1 {
		nl = nl.With(zap.String(Type, v1))
	}
	return
}
